<template>
  <el-dialog   :visible.sync="visible" width="700px" @close="handleCancel">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['receipt:receipt:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar  @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="receiptList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="工厂的回单编号" align="center" prop="receiptUuid" />
      <el-table-column label="回单是否已收" align="center" prop="isReceiptReceived">
        <template slot-scope="scope">
          <span :class="scope.row.isReceiptReceived === '1' ? 'paid' : 'not-paid'">{{ scope.row.isReceiptReceived ===
            "1" ? '已收' : '未收' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="回收日期" align="center" prop="receiveDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.receiveDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="关联企业订单的uuid编号" align="center" prop="companyOrderUuid" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['receipt:receipt:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['receipt:receipt:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工厂的回单编号" prop="receiptUuid">
          <el-input v-model="form.receiptUuid" placeholder="请输入对方企业的回单编号" />
        </el-form-item>
      <el-form-item label="回单是否已收" prop="isReceiptReceived"
        :rules="[{ required: true, message: '请选择回单状态', trigger: 'change' }]">
        <el-select v-model="form.isReceiptReceived" placeholder="请选择回单状态" clearable>
          <el-option v-for="option in getOptions" :key="option.value" :label="option.label" :value="option.value" />
        </el-select>
      </el-form-item>

        <el-form-item label="回收日期" prop="receiveDate">
          <el-date-picker clearable
            v-model="form.receiveDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择回收日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </el-dialog>
</template>

<script>
import { listReceipt, getReceipt, delReceipt, addReceipt, updateReceipt } from "@/api/fincal/receipt";

export default {
  name: "Receipt",
  props: {
    visible: {
      type: Boolean,
      required: false
    },
    orderId: {
      type: [String, Number],
      required: true
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      receiptList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        receiptUuid: null,
        companyOrderUuid: null,
        isReceiptReceived: null,
        receiveDate: null
      },
      getOptions: [
        { value: "1", label: '已收齐' },
        { value: "0", label: '未收齐' }
      ],
      // 表单校验
      rules: {
        receiptUuid: [
          { required: true, message: "回单编号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  watch: {
    visible(val) {
      if (val) {
        this.getList(this.orderId);
      }
    },
    orderId(newId, oldId) {
      if (this.visible && newId !== oldId) {
        this.getList();
      }
    }
  },
  methods: {
    /** 取消操作 */
    handleCancel() {
    this.$emit("update:visible", false);
    this.reset();
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listReceipt({companyOrderUuid:this.orderId}).then(response => {
        this.receiptList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: "",
        receiptUuid: "",
        isReceiptReceived: "",
        receiveDate: ""
      };
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加回单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getReceipt(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改回单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null && this.form.id!= "") {
            updateReceipt(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.companyOrderUuid=this.orderId;
            addReceipt(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function() {
        return delReceipt(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
<style scoped>
.paid {
  color: green;
  font-weight: bold;
  /* 可选，用于突出显示 */
}

.not-paid {
  color: red;
  font-weight: bold;
  /* 可选，用于突出显示 */
}
</style>

